package com.sgb.jicai.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.luck.picture.lib.tools.ScreenUtils;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.FragmentJoinInPurchaseBinding;
import com.sgb.jicai.entity.JoinJicaiDetailEntity;
import com.sgb.jicai.view.activity.JoinInJiCaiActivity;
import com.sgb.jicai.view.adapter.JoinInJicaiListAdapter;
import com.sgb.link.view.adapter.SelfTeamPicAdapter;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.customview.PhotoViewActivity;
import com.swgk.core.customview.ViewPagerForScrollView;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.io.Serializable;
import java.util.ArrayList;

public class JoinInPurchaseFragment extends AppLazyFragment {

    FragmentJoinInPurchaseBinding binding;

    private JoinInJicaiListAdapter listAdapter;
    private JoinJicaiDetailEntity detailEntity;

    private int pagerIndex;
    private ViewPagerForScrollView pager;
    public SelfTeamPicAdapter mPicAdapter;
    JoinInJiCaiActivity activity;

    public boolean isSingle;

    public JoinInPurchaseFragment(int pagerIndex, ViewPagerForScrollView pager, JoinInJiCaiActivity activity) {
        this.pagerIndex = pagerIndex;
        this.pager = pager;
        this.activity = activity;
    }

    public static JoinInPurchaseFragment newInstance(ViewPagerForScrollView pager, int pagerIndex, JoinInJiCaiActivity activity) {
        return new JoinInPurchaseFragment(pagerIndex, pager, activity);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_join_in_purchase, container, false);
        pager.setObjectForPosition(binding.getRoot(), pagerIndex);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.shape_list_divide_joinin_details_jc));
        if (null == listAdapter) {
            listAdapter = new JoinInJicaiListAdapter(getContext(), null);
        }
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recyclerView.setNestedScrollingEnabled(false);
        binding.recyclerView.setAdapter(listAdapter);
        binding.recyclerView.setLoadingMoreEnabled(false);
        initPic();
    }

    @Override
    protected void onLazyLoad() {
        //因为列表数据时和其它数据一起返回的，其它数据activity中需展示，数据访问放在Activity中
    }

    public void setDetailEntity(JoinJicaiDetailEntity detailEntity) {
        this.detailEntity = detailEntity;
        if (null != detailEntity) {
            if (null != detailEntity.getGroupbuyingRecordDetailList() && !detailEntity.getGroupbuyingRecordDetailList().isEmpty()) {
                listAdapter.setDatas(detailEntity.getGroupbuyingRecordDetailList());
            } else {
                listAdapter.setDatasMayNull(null);
            }
        } else {
            listAdapter.setDatasMayNull(null);
        }

        if (null == detailEntity.getDeadline() || detailEntity.getDeadline().isEmpty()) {
            listAdapter.setOfferEndtime(0);
        } else {
            listAdapter.setOfferEndtime(Long.valueOf(detailEntity.getDeadline()));
        }

        binding.leaseExpand.setEditContent(detailEntity.getRemarks());
    }

    public JoinJicaiDetailEntity getDetailEntity() {
        detailEntity.setRemarks(binding.leaseExpand.getEditContent());
        return detailEntity;
    }

    private void initPic() {
        GridLayoutManager manager = new GridLayoutManager(getContext(), 3);
        binding.rvPic.setLayoutManager(manager);
        mPicAdapter = new SelfTeamPicAdapter(getContext(), new ArrayList<>(), 1, manager);
        binding.rvPic.setHasFixedSize(false);
        binding.rvPic.addItemDecoration(new GridSpacingItemDecoration(4, ScreenUtils.dip2px(getContext(), 2), false));
        binding.rvPic.setAdapter(mPicAdapter);
        mPicAdapter.setOnItemClick(new AdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {
                if (TextUtils.isEmpty(s)) {
                    activity.setPictureSelector();
                } else {
                    Intent intent = new Intent(getActivity(), PhotoViewActivity.class);
                    intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) mPicAdapter.getDatas());
                    intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, position);
                    startActivity(intent);
                }
            }
        });
    }
}
